//
//  Status.swift
//  MyWeibo
//
//  Created by 余丽丽 on 15/10/12.
//  Copyright © 2015年 余丽丽. All rights reserved.
//
/// 微博模型

/*
    返回值字段           字段类型	字段说明
    created_at          string	微博创建时间
    id                  int64	微博ID
    idstr               string	字符串型的微博ID
    text                string	微博信息内容
    source              string	微博来源
    thumbnail_pic       string	缩略图片地址，没有时不返回此字段
    bmiddle_pic         string	中等尺寸图片地址，没有时不返回此字段
    original_pic        string	原始图片地址，没有时不返回此字段
    geo                 object	地理信息字段 详细
    user                object	微博作者的用户信息字段 详细
    retweeted_status	object	被转发的原微博信息字段，当该微博为转发微博时返回 详细
    reposts_count       int     转发数
    comments_count      int     评论数
    attitudes_count     int     表态数

*/
import UIKit

class Status: NSObject {

    //微博ID
    var id: Int = 0
    //微博创建时间
    var created_at: String?
    //微博信息内容
    var text: String?
    //微博来源
    var source: String?{
        didSet{
            //"source": "<a href=\"http://app.weibo.com/t/feed/5yiHuw\" rel=\"nofollow\">iPhone 6 Plus</a>"
            let array = source?.getFirstRegex("<a href=\"(.*?)\".*?>(.*?)</a>", options: NSRegularExpressionOptions.DotMatchesLineSeparators,regexNum:2) as [String]!
            source = "来自: " + array[1]
        }
    
    }
    //微博配图数组
    var pic_urls: [AnyObject]?
    //被转发的原微博信息字段，当该微博为转发微博时返回
    var retweeted_status: Status?
    /// 显示微博所需的行高
    var rowHeight: CGFloat?
    // 用户模型
    var user: User?
    
    //微博配图的大图URrl
    var pic_largeUrls: [AnyObject]?
    //微博配图的中涂Url
    var pic_bmiddleUrls: [AnyObject]?
    
    
    /// 构造函数
    init(dict: [String: AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
        
    }
    
    /// 加载微博信息,返回微博模型数组
    class func loadStatus(finished:(dataList:[Status]? ,error: NSError?) -> ()){
        NetworkTools.sharedTools.loadStatus { (result, error) -> () in
            if error != nil{
                finished(dataList: nil, error: error)
                return
            }
         
           if let array = result?["statuses"] as? [[String: AnyObject]] {
            
                var list = [Status]()
                for dict in array {
                    list.append(Status(dict: dict))
                }
            
                finished(dataList: list, error: nil)
            
           } else {
                finished(dataList: nil, error: nil)
            }
        
        }
    
    }
    
    
    /// 重写kvc方法,处理User字典转模型
    override func setValue(value: AnyObject?, forKey key: String) {

        if key == "user" {
            if let dict = value as? [String :AnyObject]{
            
            //换成用户模型
                user = User(dict: dict)
            }
            return
        }
        
        // 配图 KVC
        if key == "pic_urls" {
            if let array = value as? [[String: AnyObject]]{
                var picArray = [AnyObject]()
                var largePicArray = [AnyObject]()
                var bmiddlePicArray = [AnyObject]()
                for dict in array{
                    
                    let url = dict["thumbnail_pic"]!
                    let largeUrl = url.stringByReplacingOccurrencesOfString("thumbnail", withString: "large")
                    let bmiddleUrl = url.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
                    picArray.append(url)
                    largePicArray.append(largeUrl)
                    bmiddlePicArray.append(bmiddleUrl)
                    
                }
                pic_urls = picArray
                pic_largeUrls = largePicArray
                pic_bmiddleUrls = bmiddlePicArray
                
            }
            return
        }
        
        if key == "retweeted_status" {
        
            if let dict = value as? [String: AnyObject]{

                // 换成 转发模型
                retweeted_status = Status(dict: dict)
            }
            return
        }
        
        super.setValue(value, forKey: key)

        
    }
    
    /// 拦截不需要的属性进行KVC
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    /// 重写描述函数
    override var description: String{
    
        let keys = ["created_at","id","text","source","pic_urls","retweeted_status"]
        
        return "\(dictionaryWithValuesForKeys(keys))"
    }
}
    
    

